package com.yjlove.myplay.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;
import com.yjlove.myplay.view.decoration.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/11/28.
 */
public class RecyclerViewTestActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView mRecyclerview;
    private RecyclerViewAdapter mAdapter;
    private ArrayList<String> mDatas;
    private Button mAdd;
    private Button mDelete;

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_rvtest;
    }

    @Override
    protected void initView() {
        mAdd = (Button) findViewById(R.id.add);
        mDelete = (Button) findViewById(R.id.delete);
        mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview);
    }

    protected void initData() {
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }

        // 竖直排列效果
        //mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        //mRecyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        // 网格排列效果
        //mRecyclerview.setLayoutManager(new GridLayoutManager(this, 4));
        //mRecyclerview.addItemDecoration(new DividerGridItemDecoration(this));
        // 瀑布流模式（横向）
        //mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));

        // 瀑布流模式（纵向）
        mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerview.addItemDecoration(new DividerGridItemDecoration(this));
        mRecyclerview.setItemAnimator(new DefaultItemAnimator());

        mAdapter = new RecyclerViewAdapter(mDatas);
        mRecyclerview.setAdapter(mAdapter);

        mAdd.setOnClickListener(this);
        mDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                mAdapter.addData(1);
            break;
            case R.id.delete:
                mAdapter.removeData(1);
                break;
        }
        //mAdapter.notifyDataSetChanged();
    }

    private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {

        private List<Integer> heightList;

        // 生成随机宽度
        public RecyclerViewAdapter(List<String> mDatas) {
            heightList = new ArrayList<>();
            for (int i = 0; i < mDatas.size(); i++) {
                int height = new Random().nextInt(200) + 100;//[100,300)的随机数
                heightList.add(height);
            }
        }

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyHolder holder = new MyHolder(LayoutInflater.from(
                    RecyclerViewTestActivity.this).inflate(R.layout.item_rvtest, parent,
                    false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyHolder holder, final int position) {
            holder.tv.setText(mDatas.get(position));

            //由于需要实现瀑布流的效果,所以就需要动态的改变控件的高度了
            ViewGroup.LayoutParams params = holder.tv.getLayoutParams();
            params.height = heightList.get(position);
            holder.tv.setLayoutParams(params);
        }

        public void addData(int position) {
            mDatas.add(position, "Insert One");
            notifyItemInserted(position);
        }

        public void removeData(int position) {
            mDatas.remove(position);
            notifyItemRemoved(position);
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        class MyHolder extends RecyclerView.ViewHolder {

            TextView tv;

            public MyHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.id_num);
            }
        }
    }
}
